//
//  OnlineAVplay.m
//  UIAVPlayer
//
//  Created by 梁新昌 on 16/3/12.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "OnlineAVplay.h"
#import "WDGDatabaseTool.h"
#import "XCPlayViewController.h"
@implementation OnlineAVplay


+ (void)initialize {
    // 音频会话
    AVAudioSession *session = [AVAudioSession sharedInstance];
    
    // 设置会话类型（播放类型、播放模式,会自动停止其他音乐的播放）
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    
    // 激活会话
    [session setActive:YES error:nil];
    
}

-(void)play{
    
    [WDGDatabaseTool creatTableWithSQL:@"CREATE TABLE if not exists recentPlay (id integer PRIMARY KEY AUTOINCREMENT,songName text NOT NULL,singerName text, picUrl text, url text)"];
    //        根据表名获取一个对象
    
    WDGDatabaseTool *recentPlay = [WDGDatabaseTool DBManageWithTableName:@"recentPlay"];
    
    [recentPlay openDatabase];
    
    self.nameArray = [XCPlayViewController sharePlayViewController].nameArray;
    self.mutableArraySongName = [XCPlayViewController sharePlayViewController].mutableArraySongName;
    self.muatbleArraySingerName = [XCPlayViewController sharePlayViewController].muatbleArraySingerName;
    self.mutableHeadIVPicurl = [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl;
    self.selectIndex = [XCPlayViewController sharePlayViewController].selectIndex;
    //        用字典插入一行数据
    [recentPlay insertDataWithDictionary:@{@"songname": self.nameArray[_selectIndex], @"singername": self.muatbleArraySingerName[_selectIndex], @"picurl":self.mutableHeadIVPicurl[_selectIndex], @"url":self.mutableArraySongName[_selectIndex]}];
    
    
    [recentPlay closeDatabase];
    
    [super play];
    
}


+(instancetype)shareAVPlayer {
    
    static OnlineAVplay *player = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        player = [[OnlineAVplay alloc] init];
        
    });
    
    return player;
}




- (void)playWithUrl:(NSString *)url
{

    if (self.currentItem) {
        
        [self replaceCurrentItemWithPlayerItem:self.currentItem];
    }
    
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:url]];
    [self replaceCurrentItemWithPlayerItem:playerItem];
}



@end
